package com.self.structure;

public class NQueue {

    private int[] table;

    private int size = 0;

    private int takeIndex = 0;
    private int pushIndex = 0;

    public NQueue(int cap) {
        table = new int[cap];
    }

    public void push(int el) {
        if(this.size == table.length) {
            return;
        }

        this.table[pushIndex++] = el;
        this.size++;
        if(this.pushIndex == table.length) {
            this.pushIndex = 0;
        }
    }

    public int pop() {

        if(isEmpty()) {
            throw new RuntimeException("the queue is empty");
        }

        int v = this.table[takeIndex++];
        this.size--;
        if(this.takeIndex == table.length) {
            this.takeIndex = 0;
        }

        return v;

    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return this.size <= 0;
    }

    public static void main(String[] args) {
        NQueue nQueue = new NQueue(5);
        for (int i = 0 ; i< 5;i++) {
            nQueue.push(i);
            System.out.println(nQueue.pop());
        }

        while (!nQueue.isEmpty()) {
            System.out.println("遍历");
            System.out.println(nQueue.pop());
        }
    }
}
